﻿//Sort Table data , onclick=sortTable(this,event.srcElement.cellIndex)
//sortFlag 0-Ascend,1-Descend
//excepCol 1,2,3,4	exceptional column
function sortTable(obj,sCount,sortFlag,excepCol)
{
	var start=new Date;
	var theTable=new Array();
	var i;

	//var whatKey=String.fromCharCode(sortFlag);

	//unconfirmed column
	if (sCount==undefined )
	  return;

	if(sortFlag==null || sortFlag==undefined)
		sortFlag=0;

	var sExcepCol = excepCol+",";
	var sCountTmp=sCount+",";

	//this column is in exception list
	if (sExcepCol.indexOf(sCountTmp)>=0 )
	  return;

	for(i=1;i<obj.rows.length;i++)
	{
		theTable[i-1]=new Array(obj.rows(i).cells(sCount).innerText.toLowerCase(),obj.rows(i));
	}

	theTable=theTable.sort();

	if(sortFlag=='undefined')
	  sortFlag = 0;

	if(sortFlag==0)	//ascend
	{
		for(i=1;i<theTable.length;i++)
		{
			obj.lastChild.appendChild(theTable[i][1]);
		}
	}
	else	//descend
	{
		for(i=theTable.length-1;i>=0;i--)
		{
			obj.firstChild.insertBefore(theTable[i][1]);
		}
	}

	window.status=(new Date-start)+' ms';
}


//去左右两边空格
function Trim(str)
{
    var num = str.length;
    var i = 0;
    for(i = 0; i < num;i++) {
        if(str.charAt(i) != " ")
            break;
    }
    str = str.substring(i);
    num = str.length;
    for(i = num-1; i > -1;i--) {
        if(str.charAt(i) != " ")
            break;
    }
    str = str.substring(0, i+1);
    return str;
}

// 名称:全替换函数
// 功能:把指定的文本中的所有的指定的旧字符替换成指定的新字符
// 返回:已经替换的文本
// 参数:text 指定的文本
// replacement 指定的旧字符
// target 指定的新字符
function replaceAll(text,replacement,target){
    if(text==null || text=="") return text;//如果text无内容,返回text
    if(replacement==null || replacement=="") return text;//如果replacement无内容,返回text
    if(target==null) target="";//如果target无内容,设置为空串
    var returnString="";//定义返回值变量,并初始化
    var index=text.indexOf(replacement);//定义查找replacement的索引下标,并进行第一次查找
    while(index!=-1){//直至未找到replacement,要么进行下面的处理
    if(index!=0) returnString+=text.substring(0,index)+target;//如果找到的replacement前有字符,累加到返回值中,并加上target
    text=text.substring(index+replacement.length);//取掉找到的replacement及前边的字符
    index=text.indexOf(replacement);//进行查询,准备下一次处理
    }
    if(text!="") returnString+=text;//如果找到的最后一个replacement后有字符,累加到返回值中
    return returnString;//返回
}

/*格式化数字为999,999.00*/
function   formatLocalNum(num)   
{
    return parseFloat(num).toLocaleString();
} 

/*格式化数字为999999.00*/
function   formatNum(num)   
{
    return num.replace(/,/,'');
} 

//输入字符出错提示
 function KeyPressValidatorTip(tipmsg){
        var obj = document.activeElement;
        showInputValidatorTip(obj,tipmsg);
}

//输入字符出错提示
 function KeyPressValidatorLen(){
        var obj = document.activeElement;
        var maxvalue = obj.maxlength;

        if(maxvalue == undefined) {
            maxvalue = obj.maxlen;
         }
         
         //alert(obj.value.replace(/([^\x00-\xff])/g,'\x00$1').length);
        if(obj.value.replace(/([^\x00-\xff])/g,'\x00$1').length >= maxvalue)
        {
            showInputValidatorTip(obj,"超过限定长度!("+maxvalue+")");
            return false;
        }
        return true;
}

//数组方式截取汉字
function GetSubString(str, length)
{
        var temp = str;
        var j = 0;
        var k = 0;
        for (var i = 0; i < temp.length; i++)
        {
            if ( ! new RegExp("[u4e00-u9fa5]").test(temp.substr(i, 1)) )
            {
                j += 2;
            }
            else
            {
                j += 1;
            }
            if (j <= length)
            {
                k += 1;
            }
            if (j >= length)
            {
                return temp.substr(0, k);
            }
        }
        return temp;
}


//输入字符长度检查函数
//obj: 文本域或textarea
 function InputValidator(obj){
        //alert(event.srcElement.id);
        //alert(event.propertyName);
        //var obj = document.activeElement;

        var showitstr = obj.value.replace(/([^\x00-\xff])/g,'\x00$1');      //每个汉字前补空字符占位,占成substring能理解的两个字符位.
        var maxvalue = obj.getAttribute("maxlength");
        var tip = obj.getAttribute("hlpmsg");    /*显示提示帮助信息*/

        var strShowTip = "";

        if(maxvalue == undefined) {
            maxvalue = obj.getAttribute("maxlen");
         }

        if(maxvalue == undefined || showitstr == undefined) {    /*所需的信息不足(值,最大长度),无法处理*/
            if (tip != undefined && tip !="")
                strShowTip = strShowTip + tip + "&nbsp;";
                
            strShowTip = '已经输入' +  showitstr.length + '个字符';
        }
        else {
            if(showitstr.length > maxvalue)
            {
                obj.value = showitstr.substring(0,maxvalue).replace(/\x00/g,'');    //按最长度截取后,将占位符去除.
            }
            showitstr = obj.value.replace(/([^\x00-\xff])/g,'\x00$1');      //每个汉字前补空字符占位,占成substring能理解的两个字符位.

            if (tip != undefined && tip !="")
                strShowTip = strShowTip + tip + "&nbsp;";

            strShowTip = strShowTip + '剩余' +  (maxvalue - showitstr.length) + '个字符';
        }

        if(obj.getAttribute("data-type") != null)      /*可以设置data_type属性值,以便于提示该数据为什么类型*/
            strShowTip = strShowTip + '(' + obj.getAttribute("data-type") +')';

        showInputValidatorTip(obj,strShowTip);
        return true;
 }

function showInputValidatorTip(obj,value){
        var mydiv = document.getElementById("InputValidatorTipDiv");
        if(mydiv == null )     /*所需的信息不足(值,最大长度),无法处理*/
            return;

        mydiv.innerHTML='<a style="border:1px solid blue;height:30px">&nbsp;'+value+'&nbsp;</a>';
        //mydiv.style.pixelLeft=obj.clientX+document.body.scrollLeft;
        //mydiv.style.pixelTop=obj.clientY+document.body.scrollTop+10;

        var dads  = mydiv.style;

    	var ttop  = obj.offsetTop;     //TT控件的定位点高
    	var thei  = obj.clientHeight;  //TT控件本身的高
    	var tleft = obj.offsetLeft;    //TT控件的定位点宽
    	var ttyp  = obj.type;          //TT控件的类型
    	while (obj = obj.offsetParent){ttop+=obj.offsetTop; tleft+=obj.offsetLeft;}
    	dads.top  = (ttyp=="image")? ttop : ttop - 40;
    	dads.left = tleft;

        //mydiv.style.pixelLeft=obj.style.pixelLeft;
        //mydiv.style.pixelTop=obj.posTop + obj.offsetTop;
        mydiv.style.visibility="visible";
        
        //设定3秒钟后关闭提示
        //hidInputValidatorTip(5);
}

function hidInputValidatorTip(TimeDelay){
        var mydiv = document.getElementById("InputValidatorTipDiv");
        if(mydiv == null)     /*所需的信息不足(值,最大长度),无法处理*/
            return;

        if(TimeDelay == undefined) { /*未指定停顿时间,默认为不等待*/
            mydiv.style.visibility="hidden";
        }else if(TimeDelay == 0){
            mydiv.style.visibility="hidden";
        }else{
            setTimeout("hidInputValidatorTip(0)", TimeDelay * 1000);
        }

}



//==============================================================================
//Name    ：formActionSubmit(actionForm,actionUrl,formTarget)
//Function：page submit
//Params  ：actionForm-form object;actionUrl-submit to;formTarget-target
//Example : formActionSubmit(document.forms[0],"${def:context}/action/customer/cc/info/maintain/insert","_self")
//author  ：ghost
//date    ：2009-10-17
//------------------------------------------------------------------------------
function formActionSubmit(actionForm,actionUrl,formTarget)
{
    actionForm.action=actionUrl;
    actionForm.method="post";
    //actionForm.enctype="text/plain";
    actionForm.target=formTarget;
    actionForm.submit();
}